/**
 * 获取验证码组件
 * @class GetCode
 * @extends Basic
 * @author 杜慧玲
 *
 *  @param  [text] 按钮文案，默认值：获取验证码
 *
 *  @param  [count] 倒计时秒数，默认值：10
 *
 *  @param  [className] 设置class样式
 *
 *
 * 示例：
 *
 *     @example
 *     <GetCode/>
 */
var GetCode = React.createClass({
    getDefaultProps:function(){
        return{
            text:"获取验证码",
            count: 10
        }
    },
    getInitialState:function(){
        return{
            _className:this.props.className ? "ucs-btn "+this.props.className : "ucs-btn",
            _text:this.props.text,
            _disabled:false
        }
    },
    /**
     * 开始倒计时
     * @param void
     * */
    toCount:function(){
        var _this = this;
        var count = _this.props.count,
            className = _this.props.className ? "ucs-btn "+_this.props.className : "ucs-btn";
        _this.setState({
            _className : className + " ucs-btn-disabled",
            _text : count + "秒后重新获取",
            _disabled : true
        });
        _this.time = setInterval(function(){
            if(count > 0){
                count--;
                _this.setState({
                    _text : count + "秒后重新获取"
                })
            }else{
                clearInterval(_this.time);
                var GiraffeObjectManager = window.GiraffObjectManager;
                if(_this.props.dmbind){
                    GiraffeObjectManager[_this.props.dmbind].setError('clean');
                }
                _this.setState({
                    _className : className,
                    _text : _this.props.text,
                    _disabled : false
                });
            }
        },1000);
    },
    /**
     * 暂停倒计时
     * @param void
     * */
    stopCount:function(){
        var _this = this;
        this.setState({
            _className:this.props.className ? "ucs-btn "+this.props.className : "ucs-btn",
            _text:this.props.text,
            _disabled:false
        });
        clearInterval(_this.time);
    },
    render:function(){
        return(
            <button disabled={this.state._disabled} onClick={this.props.onClick} onMouseDown={this.props.onMouseDown} className={this.state._className}>{this.state._text}</button>
        )
    }
});
module.exports = GetCode;